<?php

abstract class Silk extends FP_Application {

    const INPUT_TEXT = 'Text';
    const INPUT_TEXTAREA = 'Textarea';
    const INPUT_FCK = 'FCK Rich Editor';
    const INPUT_SELECT = 'Select';
    const INPUT_CHECKBOX = 'Checkbox';
    const INPUT_RADIO = 'Radio';
    const INPUT_FILE = 'File';
    const INPUT_CUSTOM = 'Custom';

    public static function Url($pUrl){
	return rtrim(self::get('baseUrl'), '/') . $pUrl;
    }

    public static function getViewEngine(){
	$view = parent::getViewEngine();
	$view->addHelperPath(BASE_DIR . 'app/views/helpers/', 'Silk_View_Helper');
	return $view;
    }

    public static function GetCurrentSite(){
	return Silk_Application_Subsystem_SiteSwitcher::GetCurrentSite();
    }

    public static function GetDefaultObject($pSiteId=null){
	if (!$pSiteId){
	    $site = self::getCurrentSite();
	    $site_id = $site->site_id;
	} else
	    $site_id = $pSiteId;

	$Settings = new Silk_Settings();
	$default_id   = $Settings->getValue('Default Id', $site_id);

	$Objects = new Silk_Objects();

	return $Objects->fetch($default_id);

    }

    public static function GetVirtualTypeDefinition(Silk_Type $pType){
	return Silk_Application_Subsystem_ModuleLoader::GetVirtualType($pType);
    }

    public static function GetTypeDefinition($pType){
	return Silk_Application_Subsystem_ModuleLoader::GetType($pType);
    }

    public static function NewObject($pType, array $pData=array()){
	$Objects = new Silk_Objects();
	$Types   = new Silk_Types();
	$type_id    = $Types->getModelObjectId($pType);

	if ($type_id){
	    $data = array_merge($pData, array('type_id' => $type_id));
	    return $Objects->create($data);
	} else
	    throw new Silk_Exception('Invalid object type - ' . $pType);
    }


    public static function GetObject($pObject, $pType=null, $pSiteId=null){
	$Objects = new Silk_Objects();

	if (is_numeric($pObject))   //Fetch by primary key
	    return $Objects->fetch($pObject);
	else {
	    if (is_array($pObject)){  //got criteria array instead

		if ($pSiteId)
		    $site_id = $pSiteId;
		else {
		    $site = self::getCurrentSite();
		    $site_id = $site->site_id;
		}

		if ($pType){
		    $Types   = new Silk_Types();
		    $type_id    = $Types->getModelObjectId($pType);

		    if (!$type_id)
			throw new Silk_Exception('Invalid object type - ' . $pType);

		    $criteria = array_merge(array('type_id' => $type_id, 'site_id' => $site_id), $pObject);
		} else
		    $criteria = $pObject;

		$results = $Objects->fetch($criteria);
		if (count($results) > 0)
		    return $results->current();
		return null;
	    } else
		return null;
	}
    }

    public static function GetObjects(array $pCriteria, $pType=null, $pSort='object_id', $pOrder='ASC', $pSiteId=null){
	if (!$pSiteId){
	    $site = self::getCurrentSite();
	    $site_id = $site->site_id;
	} else
	    $site_id = $pSiteId;

	$Objects = new Silk_Objects();
	if ($pType){
	    $Types   = new Silk_Types();
	    $type_id = $Types->getModelObjectId($pType);
// 	    logdebug("Silk.GetObjects: Fetching objects of type [{$type->slug}] param={$pType}");
	    $criteria = array_merge(array('type_id' => $type_id, 'site_id' => $site_id), $pCriteria);
	} else
	    $criteria = array_merge(array('site_id' => $site_id), $pCriteria);

	return $Objects->fetch($criteria, null, null, "{$pSort} {$pOrder}");
    }

    public static function GetObjectPaginator($pType, array $pCriteria=array(), $pSort='object_id', $pOrder='ASC', $pSiteId=null){
	if (!$pSiteId){
	    $site = self::getCurrentSite();
	    $site_id = $site->site_id;
	} else
	    $site_id = $pSiteId;

	$Objects = new Silk_Objects();
	if ($pType){
	    $Types   = new Silk_Types();
	    $type_id    = $Types->getModelObjectId($pType);
	    $criteria = array_merge(array('type_id' => $type_id, 'site_id' => $site_id), $pCriteria);
	} else
	    $criteria = array_merge(array('site_id' => $site_id), $pCriteria);

	return $Objects->getPaginator($criteria, "{$pSort} {$pOrder}");
    }

    public static function TriggerHook($pName, $pArgs){
	Silk_Application_Subsystem_Messaging::TriggerHook($pName, $pArgs);
    }

    public static function CreateHooks(array $pHookNames){
	Silk_Application_Subsystem_Messaging::CreateHooks($pHookNames);
    }

    public static function CreateHook($pName){
	Silk_Application_Subsystem_Messaging::CreateHook($pName);
    }

    public static function RegisterCallback($pFor, $pCallback){
	return Silk_Application_Subsystem_Messaging::RegisterCallback($pFor, $pCallback);
    }

    public static function ReceiveFile($pInputName){
	$filepattern = 'asset-' . time() . rand() % 1000 . '.%ext%';
	$file = FP_File::Receive($pInputName, self::get('uploadsdir'), $filepattern);

	if ($file)
	    return array($file->path, $file->mimetype);
	else
	    return false;
    }
}

?>